package com.gsx.controller;

import com.gsx.bean.*;
import com.gsx.service.GoodService;
import com.gsx.service.InfoDetailService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Description TODO
 * @Authoe gsx
 * @Date 2021/04/07 上午 10:01
 **/
@Controller
@RequestMapping("/infoDetail")
public class InfoDetailController {
    @Resource
    private InfoDetailService infoDetailService;
    @Resource
    private GoodService goodService;

    /**
     *
     * @param gid
     * @param iDid
     * @return
     */
    @RequestMapping("/infoList")
    @ResponseBody
    public List<InfoDetail> getInfoDetailList(int gid,int iDid){
        return infoDetailService.getListByGinAndBIid(gid,iDid);
    }
    @RequestMapping("/basicInfo")
    @ResponseBody
    public List<BasicInfo> getInfoDetailList(int gid){
        return infoDetailService.getListByGin(gid);
    }

    /**
     * 去结算
     * 如果没有登陆去登陆
     * 将购物车中的信息存到session中
     * @param sum
     * @param gid
     * @param request
     * @return
     */
    @RequestMapping("/sum")
    public ModelAndView setAmount(Integer sum, Integer gid,int[] infoDetailId, HttpServletRequest request){
        HttpSession session = request.getSession();
        ModelAndView modelAndView = new ModelAndView("redirect:/html/login.html");
        User user = (User)session.getAttribute("user");
        Cart cart =null;
        if (user!=null){
             modelAndView.setViewName("/html/price.jsp");
             cart=infoDetailService.setCart(sum, gid, user.getUserId(),infoDetailId);
             modelAndView.addObject("cart",cart);
             modelAndView.addObject("good",goodService.getGoodById(gid));
        }else{
            cart=infoDetailService.setCart(sum, gid,-1,infoDetailId);
        }
        session.setAttribute("cart",cart);
        return modelAndView;

    }
}
